package com.zmj.sy.mom.srv.aps.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zmj.sy.mom.srv.aps.bean.entity.order.OrderBom;
import com.zmj.sy.mom.srv.aps.mapper.MbdDataMaintainMapper;
import com.zmj.sy.mom.srv.aps.mapper.OrderBomMapper;
import com.zmj.sy.mom.srv.mbd.api.api.MomSrvMbdApiClient;
import com.zmj.sy.mom.srv.mbd.api.bean.dto.mbdUpdata.TbSizeInfoDto;
import com.zmj.sy.mom.srv.mbd.api.bean.vo.SizeInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Service
public class MbdDataMaintainService {

    private final MomSrvMbdApiClient momSrvMbdApiClient;
    private final MbdDataMaintainMapper mbdMaintainMapper;
    private final OrderBomMapper orderBomMapper;

    /**
     * @Description: 查询工件尺寸信息
     *
     * @author: hejiahui
     * @since: 2023/10/6 8:17
     */
    public List<SizeInfoVo> getNewPartSize(String plmid) {
        List<SizeInfoVo> newPartSize = momSrvMbdApiClient.getNewPartSize(plmid);
        Set<String> plmids = newPartSize.stream().map(SizeInfoVo::getPlmid).collect(Collectors.toSet());
        if (ObjectUtils.isEmpty(plmids)) {
            return newPartSize;
        }
        //获取零件名
        List<String> apsPlmid = new ArrayList<>();
        plmids.stream().forEach(e -> {
            apsPlmid.add(e);
            if (e.endsWith("_170")) {
                apsPlmid.add(e.substring(0,e.indexOf("_170")));
            }else {
                apsPlmid.add(e + "_170");
            }
        });
        QueryWrapper partNameWrapper = new QueryWrapper();
        partNameWrapper.in("plm_id", apsPlmid);
        List<Map<String, Object>> partNames = mbdMaintainMapper.getPartName(partNameWrapper);
        Map<String, List<Map<String, Object>>> partNameMap = partNames.stream().collect(Collectors.groupingBy(e -> e.get("plm_id").toString()));

        for (SizeInfoVo vo : newPartSize) {
            if (vo.getPlmid().endsWith("_170")) {
                List<Map<String, Object>> apsList = partNameMap.get(vo.getPlmid());
                if (ObjectUtils.isEmpty(apsList)) {
                    apsList = partNameMap.get(vo.getPlmid().substring(0,vo.getPlmid().indexOf("_170")));
                }
                if (ObjectUtils.isEmpty(apsList)) {
                    vo.setPartName("");
                }else {
                    Map<String, Object> stringObjectMap = apsList.get(0);
                    vo.setPartName((String) stringObjectMap.get("mater_name"));
                }
            }else {
                List<Map<String, Object>> apsList = partNameMap.get(vo.getPlmid() + "_170");
                if (ObjectUtils.isEmpty(apsList)) {
                    apsList = partNameMap.get(vo.getPlmid());
                }
                if (ObjectUtils.isEmpty(apsList)) {
                    vo.setPartName("");
                }else {
                    Map<String, Object> stringObjectMap = apsList.get(0);
                    vo.setPartName((String) stringObjectMap.get("mater_name"));
                }
            }
        }
        return newPartSize;
    }
    /**
     * @Description: 工件尺寸信息修改接口
     *
     * @author: hejiahui
     * @since: 2024/9/27 14:58
     */
    public void updateNewPartSize(TbSizeInfoDto dto) {
        QueryWrapper partNameWrapper = new QueryWrapper();
        partNameWrapper.in("plm_id", dto.getPlmid() + "_170");
        List<Map<String, Object>> partNames = mbdMaintainMapper.getPartName(partNameWrapper);
        if (ObjectUtils.isNotEmpty(partNames)) {
            String string = (String) partNames.get(0).get("mater_name");
            //只有空的才能改
            if (StringUtils.isBlank(string)){
                orderBomMapper.lambdaUpdate().set(OrderBom::getMaterName, dto.getPartName()).eq(OrderBom::getId, partNames.get(0).get("id")).update();
            }
        }
        //修改mbd
        momSrvMbdApiClient.updateNewPartSize(dto);
    }
    /**
     * @Description: 新增组件尺寸和重量
     *
     * @author: hejiahui
     * @since: 2023/10/10 10:14
     */
    public void insertNewPartSize(TbSizeInfoDto dto) {
        if (StringUtils.isBlank(dto.getPlmid())) {
            return ;
        }
        momSrvMbdApiClient.insertNewPartSize(dto);
    }
}
